<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>透明的单例类的编写优化版</title>
</head>
<body>
<button id="loginBtn">
    登陆
</button>
<script>
var setSingle = function (fn) {
    var result;
    return function () {
        return result || (result = fn.apply(this,arguments));
    }
}
var createLoginlayer = function () {
    var div = document.createElement('div');
    div.innerHTML = '我是登陆';
    div.style.display = 'none';
    document.body.appendChild(div);
    return div;
}
var createLoginLayer = setSingle(createLoginlayer);
/*document.getElementById('loginBtn').onclick = function () {
    var loginLayer = createLoginLayer();
    loginLayer.style.display = 'block';
}*/
var createSingleframe = setSingle(()=>{
    var iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    return iframe;
});
document.getElementById('loginBtn').onclick = function () {
    var loginLayer = createSingleframe();
    loginLayer.src = 'http://baidu.com';
}
</script>
</body>
</html>